<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>点餐系统后台管理</h2>
      </div>
      <div class="login-form">
        <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              prefix-icon="el-icon-user" 
              placeholder="请输入用户名"
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              prefix-icon="el-icon-lock" 
              placeholder="请输入密码" 
              show-password
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { employeeLogin } from '@/api/employee'

export default {
  name: 'Login',
  data() {
    return {
      // 登录表单
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      // 加载状态
      loading: false
    }
  },
  methods: {
    // 处理登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          employeeLogin(this.loginForm)
            .then(res => {
              if (res.code === 1) {
                // 登录成功，保存token和用户信息
                localStorage.setItem('token', res.data.token)
                localStorage.setItem('userInfo', JSON.stringify({
                  id: res.data.id,
                  name: res.data.name,
                  userName: res.data.userName
                }))
                
                // 跳转到首页
                this.$router.push('/')
                this.$message.success('登录成功')
              } else {
                this.$message.error(res.msg || '登录失败')
              }
            })
            .catch(err => {
              console.error('登录失败', err)
              this.$message.error('登录失败，请检查网络连接')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
}

.login-button {
  width: 100%;
}
</style>